package com.tanhua.server.controller;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.UserLikeVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired//注入service
    private UsersService usersService;


    /*
        根据用户id查询用户信息
        接口路径：GET
        请求参数：Query：userID
        请求头参数：Authorization（当前操作的用户id）
        相应数据:UserInfo

        参数解析:
            userID:查询其他与和的id
            当userID!=null:表示查询其他用户的信息
            当userID =null:表示查询当前用户的信息（需要解析Authorization）
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        Long userId = userID;
        if (userId == null) {
            /*//1.判断token
            boolean verifyToken = JwtUtils.verifyToken(token);
            if (!verifyToken) {
                //如果非verifyToken,则证明用户未登录
                throw new RuntimeException("用户未登录");
            }
            //1.2解析token
            Claims claims = JwtUtils.getClaims(token);*/
            userId = UserHolder.getUserId();
        }
        //2.调用service来查询
        UserInfoVo vo = usersService.findUserInfoById(userId);
        return ResponseEntity.ok(vo);
    }

    /*
      更新用户数据
      请求方式：PUT
      请求头参数 Authorization
      请求参数: UserInfo
      响应数据：void
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        //1.接收参数
        //前端传过来的userinfo对象中没有id,所以这里需要获取userId
        Long userId = UserHolder.getUserId();
        userInfo.setId(userId);
        //2.调用service更新
        usersService.update(userInfo);
        //3.没有返回值
        return ResponseEntity.ok(null);
    }

    @GetMapping("/settings")//查询通用设置
    public ResponseEntity setting(){
        //1.获取请求参数,但前端没有传递参数
        //2.调用service来进行查询
        SettingsVo vo=usersService.settings();
        //3.返回结果
        return ResponseEntity.ok(vo);
    }

    @PostMapping("/questions")//保存陌生人问题
    public ResponseEntity questions(@RequestBody Map map){
        //1.获取请求参数
        String content = map.get("content").toString();
        //2.调用service保存
        usersService.questions(content);
        //3.返回数据
        return ResponseEntity.ok(null);
    }

    @PostMapping("/notifications/setting")//保存通用开关设置
    public ResponseEntity setting(@RequestBody Map map){
        //1.接收参数
        Boolean  like  = (Boolean)map.get("likeNotification");
        Boolean  pinglun  = (Boolean)map.get("pinglunNotification");
        Boolean  gonggao  = (Boolean)map.get("gonggaoNotification");
        //2.调用service处理数据
        usersService.setting(like,pinglun,gonggao);
        //3.返回结果
        return ResponseEntity.ok(null);
    }

    @GetMapping("/blacklist")//分页查询黑名单
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1")Integer page,
                                    @RequestParam(defaultValue = "10")Integer pagesize){
        //1.调用service处理
        PageResult pageResult=usersService.blacklist(page,pagesize);
        //2.返回参数
        return ResponseEntity.ok(pageResult);
    }

    @DeleteMapping("/blacklist/{uid}")//删除黑名单
    public ResponseEntity delete(@PathVariable("uid")String uid){
        //1.获取参数

        //2.调用service进行操作
        usersService.delete(uid);
        //3.返回数据
        return ResponseEntity.ok(null);
    }

    //修改手机号1-发送验证码
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        /**
         * 没有业务参数，也没有返回值
         */
        //调用service处理业务
        usersService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    //修改手机号2-校验验证码
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestBody Map map){
        //1.获取到前端传递的验证码
        String code = map.get("verificationCode").toString();
        //2.调用service处理
        Map codeMap=usersService.verificationCode(code);
        //3.返回参数
        return ResponseEntity.ok(codeMap);
    }

    //修改手机号3-保存新手机号
    @PostMapping("/phone")
    public ResponseEntity ModifyThePhone(@RequestBody Map map){
        //1.获取参数
        String phone = map.get("phone").toString();
        //2.调用service进行保存
        usersService.updatePhone(phone);
        //3.没有返回值
        return ResponseEntity.ok(null);
    }
    /**
     * 互相喜欢 喜欢 粉丝
     * eachLoveCount互相喜欢
     * loveCount 喜欢
     * fanCount  粉丝
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        UserLikeVo vo = usersService.counts();
        return ResponseEntity.ok(vo);
    }
    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * 路径  GET /friends/:type
     * 参数
     * type
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     * Query
     * page 当前页数
     * pagesize     * 页尺寸
     * nickname昵称
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity pageList(@PathVariable Integer type,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String nickname) {
        PageResult pageResult = usersService.pageList(type, page, pagesize, nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 粉丝--喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLikes(@PathVariable("uid") Long id) {
        usersService.fansLike(id);
        return ResponseEntity.ok(null);
    }
    /**
     * 喜欢--取消
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity fansUnLike(@PathVariable("uid") Long id){
        usersService.fansUnLike(id);
        return ResponseEntity.ok(null);
    }

}
